﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Gift.DBUtility;//请先添加引用
using Gift.Model;
using System.Collections.Generic;

namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类ProfessionAreaType。
    /// </summary>
    public class DALProfessionAreaType
    {
        public DALProfessionAreaType()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbProfessionAreaType");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbProfessionAreaType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbProfessionAreaType(");
            strSql.Append("Name,IsEnable,shopId,sortOrder,createTime,creater,description,photoUrl)");
            strSql.Append(" values (");
            strSql.Append("@Name,@IsEnable,@shopId,@sortOrder,@createTime,@creater,@description,@photoUrl)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@IsEnable", SqlDbType.Bit,1),
					new SqlParameter("@shopId", SqlDbType.Int,4),
					new SqlParameter("@sortOrder", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@creater", SqlDbType.Int,4),
					new SqlParameter("@description", SqlDbType.NVarChar,500),
					new SqlParameter("@photoUrl", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.IsEnable;
            parameters[2].Value = model.shopId;
            parameters[3].Value = model.sortOrder;
            parameters[4].Value = model.createTime;
            parameters[5].Value = model.creater;
            parameters[6].Value = model.description;
            parameters[7].Value = model.photoUrl;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbProfessionAreaType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbProfessionAreaType set ");
            strSql.Append("Name=@Name,");
            strSql.Append("IsEnable=@IsEnable,");
            strSql.Append("shopId=@shopId,");
            strSql.Append("sortOrder=@sortOrder,");
            strSql.Append("createTime=@createTime,");
            strSql.Append("creater=@creater,");
            strSql.Append("description=@description,");
            strSql.Append("photoUrl=@photoUrl");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@IsEnable", SqlDbType.Bit,1),
					new SqlParameter("@shopId", SqlDbType.Int,4),
					new SqlParameter("@sortOrder", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@creater", SqlDbType.Int,4),
					new SqlParameter("@description", SqlDbType.NVarChar,500),
					new SqlParameter("@photoUrl", SqlDbType.NVarChar,200),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.IsEnable;
            parameters[2].Value = model.shopId;
            parameters[3].Value = model.sortOrder;
            parameters[4].Value = model.createTime;
            parameters[5].Value = model.creater;
            parameters[6].Value = model.description;
            parameters[7].Value = model.photoUrl;
            parameters[8].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool isDelete(string id)
        {
            string sSQL = " select 1 from DB_Shoppro where ProfessionAreaTypeId=" + id + " union select 1 from tbCardType where ProfessionAreaTypeId=" + id;
            SqlDataReader dr = DbHelperSQL.ExecuteReader(sSQL);
            if (dr.Read())
            {
                dr.Close();
                return false;
            }
            else
            {
                dr.Close();
                return true;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete tbProfessionAreaType ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbProfessionAreaType GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Name,IsEnable,shopId,sortOrder,createTime,creater,description,photoUrl from tbProfessionAreaType ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            Gift.Model.tbProfessionAreaType model = new Gift.Model.tbProfessionAreaType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsEnable"] != null && ds.Tables[0].Rows[0]["IsEnable"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsEnable"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsEnable"].ToString().ToLower() == "true"))
                    {
                        model.IsEnable = true;
                    }
                    else
                    {
                        model.IsEnable = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["shopId"] != null && ds.Tables[0].Rows[0]["shopId"].ToString() != "")
                {
                    model.shopId = int.Parse(ds.Tables[0].Rows[0]["shopId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sortOrder"] != null && ds.Tables[0].Rows[0]["sortOrder"].ToString() != "")
                {
                    model.sortOrder = int.Parse(ds.Tables[0].Rows[0]["sortOrder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["createTime"] != null && ds.Tables[0].Rows[0]["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(ds.Tables[0].Rows[0]["createTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["creater"] != null && ds.Tables[0].Rows[0]["creater"].ToString() != "")
                {
                    model.creater = int.Parse(ds.Tables[0].Rows[0]["creater"].ToString());
                }
                if (ds.Tables[0].Rows[0]["description"] != null && ds.Tables[0].Rows[0]["description"].ToString() != "")
                {
                    model.description = ds.Tables[0].Rows[0]["description"].ToString();
                }
                if (ds.Tables[0].Rows[0]["photoUrl"] != null && ds.Tables[0].Rows[0]["photoUrl"].ToString() != "")
                {
                    model.photoUrl = ds.Tables[0].Rows[0]["photoUrl"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public tbProfessionAreaType GetModelByCardTypeId(int CardTypeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ft.Id,ft.[Name] from tbProfessionAreaType ft join tbCardType ct on ct.ProfessionAreaTypeId = ft.Id");
            strSql.Append(" where ct.Id=@CardTypeId ");
            SqlParameter[] parameters = {
					new SqlParameter("@CardTypeId", SqlDbType.Int,4)};
            parameters[0].Value = CardTypeId;

            tbProfessionAreaType model = new tbProfessionAreaType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Name,photoUrl,[name] + '[' + isnull((select CompanyShortName from tbCompany where Id = tbProfessionAreaType.companyId),'') + ']' as description  ");
            strSql.Append(" FROM tbProfessionAreaType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListByCompanyId(string companyId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select distinct pt.id,pt.[name] "
                         + " from tbCard c  join tbCardType ct on c.cardtypeid = ct.id  "
                         + " join tbProfessionAreaType pt on ct.ProfessionAreaTypeId = pt.id"
                         + " where pt.companyId = " + companyId
                         + " order by pt.[name] ");

            return DbHelperSQL.Query(strSql.ToString());
        }

        public List<tbProfessionAreaType> GetIndexMenuList(int top)
        {
            List<tbProfessionAreaType> oType = new List<tbProfessionAreaType>();
            StringBuilder strSql = new StringBuilder();
            if (top == 0)
            {
                strSql.Append("select top 8 Id,Name,photoUrl ");
            }
            else
            {
                strSql.Append("select top " + top.ToString() + " Id,Name,photoUrl ");
            }

            strSql.Append(" FROM tbProfessionAreaType where id >0 ");

            DataTable dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];

            tbProfessionAreaType type;
            foreach (DataRow dr in dt.Rows)
            {
                type = new tbProfessionAreaType();
                type.Id = int.Parse(dr["id"].ToString());
                type.Name = dr["name"].ToString();
                type.photoUrl = dr["photoUrl"].ToString();
                oType.Add(type);
            }
            return oType;

        }

        public List<tbProfessionAreaType> GetList()
        {
            List<tbProfessionAreaType> oType = new List<tbProfessionAreaType>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Name ");
            strSql.Append(" FROM tbProfessionAreaType ");

            DataTable dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];

            tbProfessionAreaType type;
            foreach (DataRow dr in dt.Rows)
            {
                type = new tbProfessionAreaType();
                type.Id = int.Parse(dr["id"].ToString());
                type.Name = dr["name"].ToString();
                oType.Add(type);
            }
            return oType;

        }


        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbProfessionAreaType";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

